package com.flycent.hqhs.ui;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.widget.ImageView;

import com.flycent.hqhs.R;
import com.flycent.hqhs.util.ContextUtil;
import com.flycent.hqhs.util.PreferenceUtil;

/**
 *  每次进入程序时显示的屏保页面
 * @author fbl
 */
public class SplashActivity extends BaseActivity {

	private static final int ANIMATION_DURATION = 2000;
	private static final float SCALE_END = 1.13F;
	private ImageView mSplashImage;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_splash);

		mSplashImage = (ImageView)findViewById(R.id.iv_entry);
		mSplashImage.setImageResource(R.drawable.splash);
		animateImage();

	}

	private void animateImage() {
		ObjectAnimator animatorX = ObjectAnimator.ofFloat(mSplashImage, "scaleX", 1f, SCALE_END);
		ObjectAnimator animatorY = ObjectAnimator.ofFloat(mSplashImage, "scaleY", 1f, SCALE_END);

		AnimatorSet set = new AnimatorSet();
		set.setDuration(ANIMATION_DURATION).play(animatorX).with(animatorY);
		set.start();

		set.addListener(new AnimatorListenerAdapter() {
			@Override
			public void onAnimationEnd(Animator animation) {
				redirectTo();
			}
		});
	}

	private void redirectTo() {
		int count = PreferenceUtil.getInt(this, "counts");
		if( count == 0){
			ContextUtil.startActivity(this, WelcomeActivity.class);
			finish();
		}else {
			ContextUtil.startActivity(this, MainTabActivity.class);
			finish();
		}

		PreferenceUtil.putInt(this, "counts", ++count);
	}


	public void onBackPressed() {}

}
